package admin

import "last/pkg/database"

type Friends struct {
	Id       int
	SiteName string `json:"site_name"`
	Desc     string `json:"desc"`
	Path     string `json:"path"`
}

// Store 新增
func (s *Friends) Store(data interface{}) bool {
	result := database.DB.Model(Friends{}).Create(data)
	return result.RowsAffected == 1
}

// Show 详情
func (s *Friends) Show(id string, data interface{}) error {
	return database.DB.Model(Friends{}).Where("id=?", id).First(&data).Error
}

// Remove 删除
func (s *Friends) Remove(id string, data interface{}) error {
	return database.DB.Model(Friends{}).Where("id=?", id).Delete(&data).Error
}

// Edit 更新
func (s *Friends) Edit(id int, data interface{}) error {
	return database.DB.Model(Friends{}).Where("id=?", id).Where("id=?", id).Updates(&data).Error
}

// List 列表
func (s *Friends) List(page, pageSize int, where map[string]interface{}) (error, int64, interface{}) {
	var total int64
	var results []Friends
	query := database.DB.Model(Friends{})
	if where["site_name"] != "" {
		query.Where("site_name=?", where["site_name"])
	}

	query.Count(&total)
	query.Offset((page - 1) * pageSize).
		Limit(pageSize)
	return query.Find(&results).Error, total, results
}
